package com.oude.httpclient;

import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.oude.httpclient.ui.activity.SettingActivity;
import com.oude.httpclient.ui.fragment.FirstFragment;
import com.oude.httpclient.ui.fragment.SecondFragment;
import com.oude.httpclient.ui.fragment.ThirdFragment;

public class MainActivity extends AppCompatActivity {

    //公共布局
    private DrawerLayout mDrawerLayout;
    private BottomNavigationBar bottomNavigationBar;
    //Fragment
    private int index;
    private int currentTabIndex=0;
    private Fragment[] mFragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initBottomNavigationBar();
        initFragment();
    }

    //初始化布局
    private void initView()
    {
        //设置标题栏
        Toolbar toolbar = findViewById(R.id.main_toolbar);
        setSupportActionBar(toolbar);
        //设置侧滑栏
        NavigationView navView = findViewById(R.id.nav_view);
        mDrawerLayout =  findViewById(R.id.mainDrawerLayout);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
        {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        navView.setCheckedItem(R.id.nav_settings);
        navView.setItemIconTintList(null);
        navView.setNavigationItemSelectedListener(new NavigationViewListener());
        //设置底部导航栏
        bottomNavigationBar = findViewById(R.id.bottom_navigation_bar);

        //设置Fragment初始化
        FirstFragment mFirstFragment = new FirstFragment();
        SecondFragment mSecondFragment = new SecondFragment();
        ThirdFragment mThirdFragment = new ThirdFragment();
        mFragments = new Fragment[]{mFirstFragment,mSecondFragment,mThirdFragment};

    }

    //toolbar加载菜单布局文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.toolbar_main, menu);
        return true;
    }

    //侧滑栏点击功能监听器
    class NavigationViewListener implements NavigationView.OnNavigationItemSelectedListener
    {
        @Override
        public boolean onNavigationItemSelected(MenuItem p1)
        {
            int id = p1.getItemId();
            switch (id)
            {
                case R.id.nav_settings:
                    Intent intent_settings =new Intent(MainActivity.this, SettingActivity.class);
                    startActivity(intent_settings);
                    break;
                default:
                    break;
            }
            return true;
        }
    }
    //侧滑栏左上角功能设置(
    //点击弹出侧滑栏
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            default:
        }
        return true;
    }

    //初始化底部导航栏
    private void initBottomNavigationBar()
    {
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        //模式跟背景的设置都要在添加tab前面，不然不会有效果。
        bottomNavigationBar
                .setActiveColor(R.color.colorPrimary)
                //默认未选择颜色
                .setInActiveColor(R.color.grey)
                //默认背景色
                .setBarBackgroundColor(R.color.white);

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.color.white, R.string.tab_first))
                .addItem(new BottomNavigationItem(R.color.white, R.string.tab_second))
                .addItem(new BottomNavigationItem(R.color.white, R.string.tab_third))
                //设置默认选择的按钮
                .setFirstSelectedPosition(0)
                .initialise();
    }

    //初始化Fragment
    private void initFragment()
    {
        //设置默认选择的Fragment
        FragmentTransaction init = getSupportFragmentManager().beginTransaction();
        init.add(R.id.fragment_main, mFragments[0]);
        init.show(mFragments[0]).commit();
        //设置lab点击事件
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position)
            {
                switch (position)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    case 2:
                        index = 2;
                        break;
                }
                if (currentTabIndex != index)
                {
                    FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
                    trx.hide(mFragments[currentTabIndex]);
                    if (!mFragments[index].isAdded())
                    {
                        trx.add(R.id.fragment_main, mFragments[index]);
                    }
                    trx.show(mFragments[index]).commit();
                }
                currentTabIndex = index;
            }

            @Override
            public void onTabUnselected(int position)
            {
            }

            @Override
            public void onTabReselected(int position)
            {
            }
        });
    }

}
